<?php

namespace app\exam\controller;

use app\common\model\Company;
use think\Request;
use think\Session;
use think\Validate;
use app\common\model\User as UserModel;

/**处理认证用户的操作行为
 * Class User
 * @package app\exam\controller
 */
class User extends Common
{
    public function index()
    {
        $user=UserModel::get(Session::get('user')['user_id']);
        $this->assign('user',$user);
//        dump(Session::get());
        return view('PersonalCenter/user');
    }
    public function examination(){
        return view('PersonalCenter/Examination');
    }

    /**编辑头像（照片）
     * @return \think\response\View
     */
    public function editAvatar()
    {
        if (Request::instance()->isGet()) {
            return view();
        } elseif (Request::instance()->isPost()) {
            $data = \request()->file('avatar');
            $info = $data->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'avatar');
//            获取当前用户模型,并更新数据
            $user = UserModel::get(Session::get('user')['user_id'])->save(['avatar' => $info->getSaveName()]);
            if ($user) {
                $this->success('修改照片成功');
            } else {
                $this->error('修改失败');
            }
        }
    }

    /**修改密码
     * @return \think\response\View
     */
    public function changePassword()
    {
        if (Request::instance()->isGet()) {
            return view();
        } elseif (Request::instance()->isPost()) {
            $data = Request::instance()->post();
            $user = UserModel::get(Session::get('user')['user_id']);
            if (md5($data['password']) == $user->password) {
                $user->save(['password' => md5($data['password'])]);
                $this->success('修改密码成功');
            } else {
                $this->error('原密码错误');
            }
        }
    }

    public function bindPhone()
    {

    }

    /**登出页面
     * @return \think\response\View
     */
    public function logout()
    {
        Session::clear();
        return redirect('/user/login');
    }


}
